#!/usr/bin/env python
#
# Copyright 2009 Benjamin Davis
#
# This file is part of resistencia.
#
# resistencia is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# resistencia is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with resistencia.  If not, see <http://www.gnu.org/licenses/>.

from items import *
from players import *

class Dungeon(object):
    """Container for all rooms and players"""

    # rooms should be a rectangular container of rooms
    rooms = [[]]
    
    def __init__(self):
        self.height = len(self.rooms)
        self.width = len(self.rooms[0])
        self.players = Players()
        x, y = 0, 0
        for row in self.rooms:
            for room in row:
                # If there is a room there
                if room is not None:
                    # Initialise the room
                    self.rooms[y][x] = room(self, x, y)
                x += 1
            x = 0
            y += 1
    
    def get_room_at_xy(self, x, y):
        """Returns the room at (x, y)"""
        return self.rooms[y][x]
    
    def draw_map(self):
        """Draw a map of the dungeon with borders."""
        output = "/" + "-" * self.width + "\\\n" 
        x, y = 0, 0
        for row in self.rooms:
            output += "|"
            for room in row:
                # If there is a room here
                if room is not None:
                    # Add a #
                    output += "#"
                else:
                    # Otherwise, add a blank space
                    output += " "
            output += "|\n"
        output += "\\" + "-" * self.width + "/"
        return output
    
class Room(object):
    """Room baseclass"""
    
    items = Items()
    
    def __init__(self, dungeon, x, y):
        self.dungeon = dungeon
        self.x, self.y = x, y
        self.players = Players()
    
    def get_relative_room(self, x, y):
        """Returns the room at (x, y) relative to self"""
        return self.dungeon.get_room_at_xy(self.x + x, self.y + y)
    
class DarkColdRoom(Room):
    
    description = "A dark, cold test room. A low-wattage incandescent lightbulb\
 hangs from the ceiling and it seems to be faintly strobing. You can hear the\
 faint sound of water dripping, but you can't seem to locate the source. There\
 is a door to the south."
    
    def __init__(self, dungeon, x, y):
        Room.__init__(self, dungeon, x, y)
        self.items = Items([SouthDoor(self), AppleSack(), Coins(100)])
        
class LighterRoom(Room):
    
    description = "A slightly lighter room. Water follows you from the north to\
 a drain at your feet You can just hear the kicking of a bass drum from another\
 room. There is a door to the north."
 
    def __init__(self, dungeon, x, y):
        Room.__init__(self, dungeon, x, y)
        self.items = Items([NorthDoor(self), Candle(), Coins(203), Duplicator()])
        
class Resistencia(Dungeon):
    """Default dungeon of resistencia"""
    
    def __init__(self):
        
        # Rooms should not be called at this stage, use Room rather than Room()
        self.rooms = [[None, None, None],
                      [None, None, DarkColdRoom],
                      [None, None, LighterRoom]]
        Dungeon.__init__(self)
